package com.cjbdi.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.cjbdi.entity.Fypz;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class FyTreeItem  implements TreeItem{
	Fypz tFypz = null;
	List<FyTreeItem> children = new ArrayList<>();
	@JsonIgnore
	FyTreeItem parent = null;
	String name ;
	public FyTreeItem(Fypz tFypz){
		this.tFypz = tFypz;
		this.name = tFypz!=null?tFypz.getCMc():null;
	}
	
	
	public void setParent(FyTreeItem parent){
		this.parent = parent;
		parent.children.add(this);
	}
	
	public List<FyTreeItem> getChildren(){
		return this.children;
	}
	
	public String getSId(){
		return tFypz.getId();
	}
	
	public Fypz getData(){
		return this.tFypz;
	}
	
	/**
	 * 查找某个指定的子功能
	 * @param functionId
	 * @return
	 */
	public FyTreeItem findChild(String functionId){
		if(tFypz.getId().equals(functionId)){
			return this;
		}
		for(FyTreeItem item:children){
			FyTreeItem find = item.findChild(functionId);
			if(find!=null){
				return find;
			}
		}
		return null;
		
	}
	
	/**
	 * 查找所有的子功能
	 * @return
	 */
	public List<FyTreeItem> findAllItem(){
		List<FyTreeItem> all = new LinkedList<>();
		findAllChildItem(all,this);
		return all;
	}
	
	public List<Long> findAllChildrenId(){
		List<FyTreeItem> items =findAllItem();
		List<Long> children = new ArrayList<Long>();
		for(FyTreeItem item:items){
			children.add(item.getId());
		}
		return children;
	}
	
	
	private void findAllChildItem(List<FyTreeItem> all,FyTreeItem parent){
		for(FyTreeItem item:parent.children){
			all.add(item);
			findAllChildItem(all,item);
		}
		
	}
	
	
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((tFypz == null) ? 0 : tFypz.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FyTreeItem other = (FyTreeItem) obj;
		if (tFypz == null) {
			if (other.tFypz != null)
				return false;
		} else if (!tFypz.equals(other.tFypz))
			return false;
		return true;
	}


	@Override
	public String toString() {
		return "MenuItem [sysFunction=" + tFypz.getCMc() + ","+this.children.size()+"]";
	}


	@Override
	public String getName() {
		return tFypz==null?"":tFypz.getCMc();
	}


	@Override
	public Long getId() {
		// TODO Auto-generated method stub
		return null;
	}



	
	
	
	

}
